/*
 * @lc app=leetcode.cn id=56 lang=golang
 *
 * [56] 合并区间
 */

// @lc code=start
// 贪心

import "sort"

func merge(intervals [][]int) [][]int {
	sort.Slice(intervals, func(i, j int) bool {
		return intervals[i][0] < intervals[j][0]
	})
	res := make([][]int, 0, len(intervals))
	res = append(res, intervals[0])
	index := 0
	n := len(intervals)
	for i := 1; i < n; i++ {
		if intervals[i][1] <= res[index][1] {
			continue
		} else {
			if intervals[i][0] > res[index][1] {
				index++
				res = append(res, intervals[i])
			} else {
				res[index][1] = intervals[i][1]
			}
		}
	}
	return res
}

// @lc code=end
